package com.web.oa.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.oa.mapper.EmployeeMapper;
import com.web.oa.mapper.EmployeeMapper2;
import com.web.oa.mapper.SysRoleMapper;
import com.web.oa.mapper.SysUserRoleMapper;
import com.web.oa.pojo.Employee;
import com.web.oa.pojo.EmployeeExample;
import com.web.oa.pojo.SysUserRole;
import com.web.oa.pojo.SysUserRoleExample;
import com.web.oa.service.EmployeeService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeMapper2 employeeMapper2;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;



    @Override
    public Employee findEmployeeByName(String name) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        //返回一个员工对象
        List<Employee> employees = employeeMapper.selectByExample(example);
        if (employees != null && employees.size() > 0) {
            return employees.get(0);
        }
        return null;
    }

    @Override
    public PageInfo<Employee> findAllUserAndRole(int pageNum) {
        PageHelper.startPage(pageNum,5);
        List<Employee> employees = employeeMapper2.selectAllUserAndRole();
        return new PageInfo<>(employees);
    }

    @Override
    public void updateEmployeeRole(String roleId, String userId) {
        //先根据userId得到一条数据
        SysUserRoleExample example = new SysUserRoleExample();
        SysUserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andSysUserIdEqualTo(userId);
        SysUserRole sysUserRole = sysUserRoleMapper.selectByExample(example).get(0);
        //改了roleId，然后更新
        sysUserRole.setSysRoleId(roleId);
        sysUserRoleMapper.updateByPrimaryKey(sysUserRole);
    }

    @Override
    public void addEmployee(Employee employee) {

        //设置密文
        String salt = "eteokues";
        Md5Hash newPassword = new Md5Hash(employee.getPassword(), salt, 2);
        employee.setPassword(String.valueOf(newPassword));
        employee.setSalt(salt);
        employee.setManagerId(employee.getManagerId());
        employeeMapper.insert(employee);

        SysUserRole role = new SysUserRole();
        role.setId(String.valueOf(employee.getId()));
        role.setSysUserId(employee.getName());
        role.setSysRoleId(employee.getRole()+"");
        sysUserRoleMapper.insertSelective(role);
    }

    @Override
    public Employee findEmployeeManager(Long employeeId) {
        return employeeMapper.selectByPrimaryKey(employeeId);
    }

    @Override
    public void deleteRoleByRoleId(String id) {
        sysRoleMapper.deleteByPrimaryKey(id);
    }
}
